package com.cwt_taxi_xa.util;

import android.os.RemoteException;

import com.cwt_taxi_xa.MainApplication;
import com.cwt_taxi_xa.constants.MyStatus;
import com.cwt_taxi_xa.constants.SPConstants;
import com.cwt_taxi_xa.protocol.MCUUtil;
import com.iflytek.speech.ISpeechModule;
import com.iflytek.speech.InitListener;
import com.iflytek.speech.SpeechConstant;
import com.iflytek.speech.SpeechSynthesizer;
import com.iflytek.speech.SynthesizerListener;

/**
 * tts功能
 * @author ferch
 *
 */
public class TTsFunction{
	static String TAG = "TTsFunction";
	public static SpeechSynthesizer mTts;
	public static boolean TTS_Playing = false;

	/**
	 * 初始化TTS语音
	 */
	public static void initTTS() {
		// TODO Auto-generated method stub
		mTts = new SpeechSynthesizer(MainApplication.getInstance(), new InitListener() {

			@Override
			public void onInit(ISpeechModule arg0, int arg1) {
				// TODO Auto-generated method stub
				if(arg1==0){
					LogUtils.e("TTS初始化成功");
				}else{
					LogUtils.e("TTS初始化失败---code = " + arg1);
				}
			}
		});
		mTts.setParameter(SpeechConstant.ENGINE_TYPE, "local");
		mTts.setParameter(SpeechSynthesizer.SPEED, "50");
		mTts.setParameter(SpeechSynthesizer.PITCH, "50");
		mTts.setParameter(SpeechSynthesizer.VOICE_NAME, "xiaoyan");
		mTts.setParameter(SpeechSynthesizer.VOLUME, "50");
	}

	/**
	 * TTs播报
	 * @param text
	 */
	public static int lastvic = 6;
	public static boolean ttsvic = false;
	public static void speakText(final String text) {
		if(MyStatus.vichl!=2){
			MyStatus.lastvichl = MyStatus.vichl;
			MCUUtil.selecterVic(2);
		}
		if(!ttsvic){
			ttsvic = true;
			lastvic = SPUtils.getParamInteger(SPConstants.SPK_vol, SPConstants.SPV_vol);
			AppLog.d(TAG,"speakText" + lastvic);
			MCUUtil.setvol(SPUtils.getParamInteger(SPConstants.SPK_vol_voice, SPConstants.SPV_vol_voice), "语音播报前强制音量6");
		}
		speakTextDelay(text, 300);
//		MainApplication.handler.post(new Runnable() {
//			@Override
//			public void run() {
//				// 关闭上一条语音播报
//				TTsFunction.stopSpeak();
//
//				LogUtils.v(String.valueOf(text));
//				try {
//					if(MyStatus.vichl!=2){
//						MyStatus.lastvichl = MyStatus.vichl;
//						MCUUtil.selecterVic(2);
//					}
//					mTts.startSpeaking(text, mTtsListener);
//				} catch (Exception e) {
//					e.printStackTrace();
//				}
//			}
//		});
	}

	/**
	 * 延时TTS播报
	 * @param text
	 * @param delay
	 */
	public static void speakTextDelay(final String text, final long delay) {
		MainApplication.handler.postDelayed(new Runnable() {
			@Override
			public void run() {
				// 关闭上一条语音播报
				TTsFunction.stopSpeak();
				LogUtils.v(String.valueOf(text));
				try {
					mTts.startSpeaking(text, mTtsListener);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}, delay);
	}

	/**
	 * TTS回调监听。
	 */
	static SynthesizerListener mTtsListener = new SynthesizerListener.Stub() {
		@Override
		public void onBufferProgress(int progress) throws RemoteException {
		}
		@Override
		public void onCompleted(int code) throws RemoteException {
			// 语音播报结束
			TTS_Playing = false;
			if(MyStatus.lastvichl!=2){
				MCUUtil.selecterVic(MyStatus.lastvichl);
				MyStatus.lastvichl = 2;
			}
			ttsvic = false;
			MCUUtil.setvol(lastvic, "语音播报结束恢复音量");
		}
		@Override
		public void onSpeakBegin() throws RemoteException {
			// 开始播报
			TTS_Playing = true;

		}
		@Override
		public void onSpeakPaused() throws RemoteException {
			// 暂停
			TTS_Playing = false;
		}
		@Override
		public void onSpeakResumed() throws RemoteException {
			// 重新播放回调
			TTS_Playing = true;
		}
		@Override
		public void onSpeakProgress(int progress) throws RemoteException {
			// 播放进度回调
		}
	};

	public static void stopSpeak() {
		mTts.stopSpeaking(mTtsListener);
		TTS_Playing = false;
		try {
			Thread.sleep(100);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

}
